from flask import Flask, session, g
import config
from exts import db
from blueprints import image_bp, patient_bp, user_bp
from flask_migrate import Migrate
from models import *

app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
app.register_blueprint(image_bp)
app.register_blueprint(patient_bp)
app.register_blueprint(user_bp)
migrate = Migrate(app, db)

if __name__ == '__main__':
    app.run()


@app.before_request
def before_request():
    user_id = session.get('user_id')
    if user_id:
        try:
            user = Users.query.get(user_id)
            g.user = user
        except:
            pass


# 先执行before_request,然后再执行视图函数，返回模板时执行context_processor

@app.context_processor
def context_processor():
    if hasattr(g, 'user'):
        return {'user': g.user}
    else:
        return {}
